﻿
using Furion.Schedule;

namespace GnpFine.Application.NmpaSystem;

/// <summary>
/// 清理日志作业任务
/// </summary>
[JobDetail("NmpaSaleJob", Description = "药品销售", GroupName = "default", Concurrent = false)]
[PeriodHours(8, TriggerId = "trigger_NmpaSaleJob", Description = "药品销售", MaxNumberOfRuns = 5, RunOnStart = true, StartNow = true)]
public class NmpaSaleJob : IJob
{
    private readonly ISqlSugarClient _db;
    public NmpaSaleJob(ISqlSugarClient db)
    {
        _db = db;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {

        Log.Information("\n-----------药品销售更新--任务开始----------");

        // 获取定时任务的参数
        var requestParam = context.JobDetail.GetProperty("sqlwhere").ToString().Trim();
        
        // 从金蝶服务器获取数据
        ResponseModel<SALE> kDCusinvdata = KdCloudOpertionUtils.GetCusdata<SALE>("GnFine.Yaojian.WebAPI.ServicesStub.GetCustSale.ExecuteService,GnFine.Yaojian.WebAPI.ServicesStub", requestParam);
        
        // 更新数据库
        var _childDb = _db.AsTenant().GetConnectionScopeWithAttr<SALE>();
        var a = _childDb.Ado.ExecuteCommand("delete  from [SALE] ");
        //var b = _childDb.AsTenant().InsertableWithAttr(kDCusinvdata.data).ExecuteCommand();
        //更换为大数据操作，13万直接把程序卡死了。//性能比 EFCore.Bulkextension 快30% （列越多差距越大）
        var b =_childDb.Fastest<SALE>().BulkCopy(kDCusinvdata.data);
   
   
        Log.Information($"药品销售更新--任务结束:数据获取{kDCusinvdata.rowcount}条,数据插入{b}条，删除{a}条\n");

    }
}

